package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.OrderMapper;
import com.heima.pojo.dto.OrderCountDTO;
import com.heima.pojo.entity.Order;
import com.heima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * \* User: chy
 * \* Date: 2025/8/23  8:33
 * \* Description:
 * \
 */
@Service
public class ReportServiceImpl extends ServiceImpl<OrderMapper,Order> implements ReportService {

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 查询订单统计数据
     * @param orderCountDTO
     * @return
     */
    @Override
    public Integer orderCount(OrderCountDTO orderCountDTO) {

        //查询订单统计数据

        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<Order>()
                .ge(Order::getCreateTime,orderCountDTO.getStart())
                .le(Order::getCreateTime,orderCountDTO.getEnd())
                .eq(Order::getStatus,Order.ORDER_STATUS_SHIP_SUCCESS);
        Integer orderCount = this.count(wrapper);
        return  orderCount;

    }

    /**
     * 查询订单金额统计数据
     * @param orderCountDTO
     * @return
     */
    @Override
    public BigDecimal orderAmount(OrderCountDTO orderCountDTO) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<Order>()
                .ge(Order::getCreateTime, orderCountDTO.getStart())
                .le(Order::getCreateTime, orderCountDTO.getEnd())
                .eq(Order::getStatus, Order.ORDER_STATUS_SHIP_SUCCESS);

        // 查询符合条件的订单列表并计算总金额
        List<Order> orders = this.list(wrapper);
        // 计算总金额
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (Order order : orders) {
            totalAmount = totalAmount.add(order.getAmount());
        }

        return totalAmount;


    }
}
